package com.by.wx.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class UtilsA {

	private static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	private static final String APPID ="wxfdd043128cf84acd";
	private static final String APPSECRET="1b1c550e10b62a2aeba8238627f64eb6";
	private static final String APPKEY= "*****";
	
	/**
	 * 向指定的地址发送一个post请求，带着data数据
	 * @param url
	 * @param data
	 * @return
	 */
	public static String post(String url,String data) {
		try {
			URL urlObj = new URL(url);
			URLConnection connection = urlObj.openConnection();
			//设置可发送数据状态，默认为false
			connection.setDoOutput(true);
			OutputStream os = connection.getOutputStream();
			os.write(data.getBytes());
			os.close();
			InputStream is = connection.getInputStream();
			byte [] b = new byte[1024];
			int len;
			StringBuffer sb = new StringBuffer();
			while((len=is.read(b))!=-1){
				sb.append(new String(b,0,len));
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String get(String url) {
		try {
			URL urlObj = new URL(url);
			URLConnection connection = urlObj.openConnection();
			InputStream is = connection.getInputStream();
			byte [] b = new byte[1024];
			int len;
			StringBuffer sb = new StringBuffer();
			while((len=is.read(b))!=-1){
				sb.append(new String(b,0,len));
			}
			return sb.toString();
			} catch (Exception e) {
				e.printStackTrace();
		}
		return null;
	}
	
	
	
}
